﻿// JScript 文件

var TD;
var OldValue;

Ext.onReady(function () {

    Ext.QuickTips.init(); //初始化接点提示
    
    this.AddMaterial = function _AddMaterial()
    {
        var  submitUrl = 'ashx/SizeMate_Add.ashx '  ;
        Ext.getBody().mask();
        var addWin = new Ext.Window({
            width: 340,
            autoHeight: true,
            title: "添加类型",
            maximizable: true,
            items: [{
                xtype: "form",
                labelWidth: 80,
                labelAlign: 'right',
                url: submitUrl,
                border: false,
                baseCls: 'x-plain',
                bodyStyle: 'padding:5px 5px 0',
                anchor: '100%',
                defaults: {
                    width: 300,
                    msgTarget: 'side'
                },
                items: [{
                    xtype: "fieldset",
                    title: "类型名称",
                    autoHeight: true,
                    items: [{
                        xtype: "textfield",
                        fieldLabel: "类型名称",
                        name: "mat.Name"
                    },{

                    xtype : "combo",
                    fieldLabel: "单位",
                    allowblank:false,
                    editable:false,
                    mode : 'local',
					triggerAction : 'all',
                    name: "mat.Unit",
                    store: [ ["厘米","厘米"],["尺寸","尺寸"]],
                    value:"厘米"
                    }]
                }],
                buttonAlign: 'right',
                minButtonWidth: 60,
                buttons: [{
                    text: '添加',
                    handler: function (btn) {
                        var frm = this.ownerCt.ownerCt.form;
                        if (frm.isValid()) {
                            btn.disable();
//                            var cnfield = frm.findField('mat.Name');
//                            var unit = frm.findField('mat.Unit');
                            frm.submit({
                                waitTitle: '请稍候',
                                waitMsg: '正在提交表单数据,请稍候...',
                                success: function (form, action) {
//                                    var id = Ext.util.JSON.decode(response.responseText).msg;
//
//                                    var sel = document.getElementById("sizeMaterial");
//                                    var opt = new Option(cnfield,id);
//                                    opt.setAttribute("unit",unit);
//                                    sel.options[sel.length] =opt;
//                                    sel.selectedIndex = sel.length-1; 
                                    LoadAllMat();
                                    addWin.close();
                                    Ext.getBody().unmask();   
                                   // tree.getRootNode().reload();
                                },
                                failure: function () {
                                    Ext.Msg.show({
                                        title: '错误提示',
                                        width: 200,
                                        msg: '添加失败!',
                                        buttons: Ext.Msg.OK,
                                        fn: function () {
                                            btn.enable();
                                        },
                                        icon: Ext.Msg.ERROR
                                    });
                                   // Ext.getBody().unmask(); 
                                }
                            });
                        }
                    }
                }, {
                    text: '重置',
                    handler: function () {
                        this.ownerCt.ownerCt.form.reset();
                    }
                }, {
                    text: '取消',
                    handler: function () {
                        this.ownerCt.ownerCt.ownerCt.hide();
                        Ext.getBody().unmask();
                    }
                }]
            }]
        });
        addWin.show();
        addWin.on("beforeclose",function (win){
              Ext.getBody().unmask();      
             },this);
    };
    this.EditMaterial = function _EditMaterial()
    {
        var  submitUrl = 'ashx/SizeMate_Update.ashx ';
        Ext.getBody().mask();
        var selMat=document.getElementById("sizeMaterial");
        var matId=selMat.value;
        var matName = selMat.options[selMat.selectedIndex].text;
        var matUnit = selMat.options[selMat.selectedIndex].getAttribute("unit");
      
        var editWin = new Ext.Window({
            width: 340,
            autoHeight: true,
            title: "修改类型",
            maximizable: true,
            items: [{
                xtype: "form",
                labelWidth: 80,
                labelAlign: 'right',
                url: submitUrl,
                border: false,
                baseCls: 'x-plain',
                bodyStyle: 'padding:5px 5px 0',
                anchor: '100%',
                defaults: {
                    width: 300,
                    msgTarget: 'side'
                },
                items: [{
                    xtype: "fieldset",
                    title: "类型名称",
                    autoHeight: true,
                    items: [{
                        xtype: "textfield",
                        fieldLabel: "类型名称",
                        name: "mat.Name",
                        value:matName
                    },{
                        xtype:"hidden",
                        name:"mat.id",
                        value:matId
                        
                    },{

                        xtype : "combo",
                        fieldLabel: "单位",
                        allowblank:false,
                        editable:false,
                        mode : 'local',
					    triggerAction : 'all',
                        name: "mat.Unit",
                        store: [ ["厘米","厘米"],["尺寸","尺寸"]],
                        value:matUnit
                    }]
                }],
                buttonAlign: 'right',
                minButtonWidth: 60,
                buttons: [{
                    text: '修改',
                    handler: function (btn) {
                        var frm = this.ownerCt.ownerCt.form;
                        if (frm.isValid()) {
                            btn.disable();
//                            var cnfield = frm.findField('mat.Name');
//                            var unit = frm.findField('mat.Unit');
                            frm.submit({
                                waitTitle: '请稍候',
                                waitMsg: '正在提交表单数据,请稍候...',
                                success: function (form, action) {
                                    var _matName = frm.findField('mat.Name').getValue();
                                    var _unit = frm.findField('mat.Unit').getValue();
                                    var _id = frm.findField('mat.id').value;
                                    document.getElementById("sizeMatUnit").value = _unit;
                                    selMat.options[selMat.selectedIndex].text = _matName;
                                    selMat.options[selMat.selectedIndex].setAttribute("unit",_unit);
//                                    var opt = new Option(_matName,_id);
//                                    opt.setAttribute("unit",_unit);
//                                    selMat.options.remove(selMat.selectedIndex); 
                                    //selMat.options.add(opt);
                                    editWin.close();
                                    Ext.getBody().unmask();    
                                },
                                failure: function () {
                                    Ext.Msg.show({
                                        title: '错误提示',
                                        width: 200,
                                        msg: '添加失败!',
                                        buttons: Ext.Msg.OK,
                                        fn: function () {
                                            btn.enable();
                                        },
                                        icon: Ext.Msg.ERROR
                                    });
                                   // Ext.getBody().unmask(); 
                                }
                            });
                        }
                    }
                },  {
                    text: '取消',
                    handler: function () {
                        this.ownerCt.ownerCt.ownerCt.hide();
                        Ext.getBody().unmask();
                    }
                }]
            }]
        });
        editWin.show();
        editWin.on("beforeclose",function (win){
              Ext.getBody().unmask();      
             },this);
    };
    this.LoadAllSizeValue = function _LoadAllSizeValue()
    {
        var matID = document.getElementById("sizeMaterial").value;
        Ext.Ajax.request({
            url:'ashx/Size_GetAll.ashx',
             params: {
                MatId: matID
             },
            success:function(response){
                var obj = Ext.util.JSON.decode(response.responseText);
                if(obj=="非法操作")
                {
                    Ext.MessageBox.show({
                        title:"错误提示",
                        msg:"非法操作",
                        buttons:Ext.MessageBox.OK,
                        icon:Ext.MessageBox.ERROR
                    })
                    return;
                }
                
                var tbl = document.getElementById("tblSize");
                var trs = tbl.rows;
                
                var header = trs[0].cells;
                for(var i=0;i<12;i++)
                {
                    header[i+2].innerHTML = obj.Header[i];
                }
                for(var i=0;i<15;i++)
                {
                    trs[i+1].cells[1].innerHTML = obj.Rows[i];
                }
                for(var i=0;i<15;i++)
                {
                    for(var j=0;j<12;j++)
                    {
                        trs[i+1].cells[j+2].innerHTML = "";
                    }
                }
                for(var i=0;i<obj.Vals.length;i++)
                {
                    var sizeVal = obj.Vals[i];
                    var colNO = parseInt(sizeVal.sizeColID)  ;
                    var sizeNO = parseInt(sizeVal.sizeID) + 1;
                    trs[colNO].cells[sizeNO].innerHTML = sizeVal.sizeValue;
                }
                
            },
            failure:function(){
                Ext.MessageBox.show ({
                    title:"错误提示",
                    msg:"系统错误",
                    buttons:Ext.MessageBox.OK,
                    icon:Ext.MessageBox.ERROR
                })
            } 
        })
    };
    this.LoadAllMat = function _LoadAllMat()
    { 
        Ext.Ajax.request({
            url:'ashx/SizeMate_GetAll.ashx',
            success:function(response){
                var obj = Ext.util.JSON.decode(response.responseText);
                if(obj=="非法操作")
                {
                    Ext.MessageBox.show({
                        title:"错误提示",
                        msg:"非法操作",
                        buttons:Ext.MessageBox.OK,
                        icon:Ext.MessageBox.ERROR
                    })
                    return;
                }
                var SelMat = document.getElementById("sizeMaterial");
                var SelUnit = document.getElementById("sizeMatUnit");
                SelMat.length=0;
                for(var i=0;i<obj.length;i++)
                {   
                    var opt = new Option(obj[i].sizeMatName,obj[i].sizeMatId);
                    opt.setAttribute("unit",obj[i].sizeUnit);
                    SelMat.options[SelMat.length] =opt;
                    SelMat.selectedIndex = SelMat.length-1;
                    
                    //alert(obj[i].sizeMatName);
                } 
                if(obj.length>0)
                {
                    SelUnit.value = SelMat.options[SelMat.selectedIndex].getAttribute("unit");
                    LoadAllSizeValue();
                }
            },
            failure:function(){
                Ext.MessageBox.show ({
                    title:"错误提示",
                    msg:"系统错误",
                    buttons:Ext.MessageBox.OK,
                    icon:Ext.MessageBox.ERROR
                })
            } 
        })
    };
    this.AddSize = function _AddSize(colId,rowId,val)
    {
        var matID = document.getElementById("sizeMaterial").value;
        if(matID == "")
        {
            alert("参数错误！没有选择尺寸种类");
            return;
        } 
        var myMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "Please wait..."
        });
        myMask.show();
        Ext.Ajax.request({
            url:'ashx/Size_Add.ashx',
            params: {
                MatId: matID,
                RowId:rowId,
                ColId:colId,
                Val:val
            },
            success:function(response) {
                var success = Ext.util.JSON.decode(response.responseText).success;
                var msgRet = Ext.util.JSON.decode(response.responseText).msg;
                if (success) {
                    myMask.hide();
                    TD.innerHTML = val;
                    RetsetState();
//                    Ext.MessageBox.show({
//                        title: '成功提示',
//                        msg: '更新成功',
//                        buttons: Ext.MessageBox.OK,
//                        icon: Ext.MessageBox.INFO,
//                        fn: function () {
//                            TD.innerHTML = val;
//                            RetsetState();
//                            
//                        }
//                    });
                } else {
                    myMask.hide();
                    RetsetState();
                    Ext.MessageBox.show({
                        title: '失败提示',
                        msg:msgRet,
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.ERROR
                    });

                }
            },
            failure: function () {
                myMask.hide();
                Ext.MessageBox.show({
                    title: '失败提示',
                    msg: '系统错误，请联系管理员',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.ERROR
                });

            }
         })
    }
    this.delSizeMat = function _delSizeMat () 
    {
        var matID = document.getElementById("sizeMaterial").value;
        if(matID == "")
        {
            alert("请选择要删除的记录！");
            return;
        } 
        var myMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "Please wait..."
        });
        myMask.show();
        Ext.Ajax.request({
            url: 'ashx/SizeMate_Del.ashx',
            params: {
                id: matID
            },
            success: function (response) {
    
                var success = Ext.util.JSON.decode(response.responseText).success;
                var msgRet = Ext.util.JSON.decode(response.responseText).msg;
                if (success) {
                    myMask.hide();
                    Ext.MessageBox.show({
                        title: '成功提示',
                        msg: '删除成功',
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.INFO,
                        fn: function () {
                            LoadAllMat();
                        }
                    });
                } else {
                    myMask.hide();
                    Ext.MessageBox.show({
                        title: '失败提示',
                        msg:msgRet,
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.ERROR
                    });

                }
            },
            failure: function () {
                myMask.hide();
                RetsetState();
                
                Ext.MessageBox.show({
                    title: '失败提示',
                    msg: '系统错误，请联系管理员',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.ERROR
                });

            }
        });
        
        
  
    }
    
    LoadAllMat();
    
});

function RetsetState()
{
    hideAllMask();
    document.getElementById("txtValue").style.visibility = "hidden";
}
function DoSizeMatChange()
{
    var SelSizeMat = document.getElementById("sizeMaterial");
    if(SelSizeMat==null) return;
    var unit = SelSizeMat.options[SelSizeMat.selectedIndex].getAttribute("unit");
    if(unit == null) return;
    document.getElementById("sizeMatUnit").value = unit;
    LoadAllSizeValue();
}
function getPosition(obj, toElement)
{
	var point = new Object();
	
	var Left = obj.offsetLeft;
	var Top  = obj.offsetTop;
	var objParent = obj.offsetParent;
	while (objParent && objParent != toElement)
	{
		Left += objParent.offsetLeft;
		Top  += objParent.offsetTop;
		objParent = objParent.offsetParent;
	}
	point.x = Left;
	point.y = Top;
	return point;
}

function DoModifyValue(obj)
{
    TD = obj;
    var objWidth = obj.offsetWidth;
    var objHeight = obj.offsetHeight;
    var pt = getPosition(obj);
    var txtValue = document.getElementById("txtValue");
    
    txtValue.value = obj.innerHTML;
    txtValue.style.width = objWidth-4;
    txtValue.style.height = objHeight-4;
    txtValue.style.left = pt.x;
    txtValue.style.top = pt.y;
    txtValue.style.visibility = "visible";
    txtValue.select();
    OldValue = txtValue.value;
    //txtValue.offsetLeft =obj.offsetLeft;
  showAllMask();
  
} 
function DoCommitValue(obj)
{
    var val = obj.value;
    if(val != OldValue)
    {
//        alert(val);
//        alert(OldValue);
        var matId = document.getElementById("sizeMaterial").value;
        if(matId == null || matId == "")
        {
            alert("请首先选择一个尺寸的种类！");
            
        }
        else
        {
            colId = TD.getAttribute("colId");
            rowId = TD.parentNode.getAttribute("rowId");
            AddSize(colId,rowId,val);
             
        }
    }
    else
    {
        RetsetState();
    }
    

}